NYC Restaurant Inspections

library(tidyverse)
library(p8105.datasets)

library(plotly)
library(dplyr)
data(rest_inspec)

rest_inspec =
  rest_inspec |> 
  janitor::clean_names() |> 
  separate(record_date, into = c('record_year', 'record_month', 'record_day'), remove = TRUE) |> 
  filter(
    record_year == "2017",
    !is.na(score),
    !is.na(grade)
  )
## Warning: Expected 3 pieces. Additional pieces discarded in 397584 rows [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
## 18, 19, 20, ...].

Scores by Borough

rest_inspec_plot1 = 
  rest_inspec |> 
  filter(
    !is.na(score),
    !is.na(boro),
    boro != "Missing"
  ) |> 
   plot_ly(x = ~boro, y = ~score, type = 'box') |> 
  layout(
    title = "Inspection Scores by Borough",
    xaxis = list(title = "Borough"),
    yaxis = list(title = "Inspection Score"),
    subtitle = "Comparing inspection scores across NYC boroughs"
  ) 

rest_inspec_plot1
## Warning: 'layout' objects don't have these attributes: 'subtitle'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'

Grades in ZIP 10032

rest_inspec_plot2 = 
  rest_inspec |> 
  filter(
    !is.na(grade),
    zipcode == "10032"
  ) |> 
  mutate(
    grade = str_replace(grade, "^N$", "Not Yet Graded"),
    grade = str_replace(grade, "^A$", "Grade A"),
    grade = str_replace(grade, "^B$", "Grade B"),
    grade = str_replace(grade, "^C$", "Grade C"), 
    grade = str_replace(grade, "^Z$", "Grade Pending"),
    grade = str_replace(grade, "^P$", "Pending Re-opening")
  ) |> 
   group_by(grade) |> 
  summarise(n = n()) |> 
  plot_ly(labels = ~grade, values = ~n,type = 'pie'
) |> 
  layout(
    title = 'Distribution of Restaurant Grades in ZIP Code 10032',
    xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
    yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
  )

rest_inspec_plot2

Number of Restaurant Inspections Over Time

rest_inspec_plot3 =
  rest_inspec |> 
  mutate(
    year_month = cut(inspection_date, breaks = "month")
  ) |> 
  count(year_month) |> 
  plot_ly(
    x = ~year_month,
    y = ~n,
    type = "scatter",
    mode = "lines+markers"
  ) |> 
  layout(
    title = "Number of Restaurant Inspections Over Time",
    xaxis = list(title = "Date"),
    yaxis = list(title = "Number of Inspections")
  )

rest_inspec_plot3